package com.gzhryc.shared_device.oem.h5.mng.controllers.market.cloud;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.config.DefaultEnum;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.TreeResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.goods.dao.db.GoodsType;
import com.gzhryc.net_marketing.cloud.goods.services.GoodsTypeService;
import com.gzhryc.net_marketing.cloud.goods.services.dto.GoodsTypeSearch;
import com.gzhryc.net_marketing.core.enums.EGoodsClassify;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class GoodsTypeController {

    @Route(value="/market/cloud/goods/type/list.html")
    public String list(HttpServletRequest request){
        request.setAttribute("stateList", DefaultEnum.Status.values());
        request.setAttribute("classifyList", EGoodsClassify.values());
        return "/market/cloud/goods/type/list.html";
    }

    @Route(value="/market/cloud/goods/type/list.data")
    public ListResult<GoodsType> listData(HttpServletRequest request){
        ListResult<GoodsType> result = new ListResult<GoodsType>(DtoConstants.SUCCESS, "暂无数据");
        GoodsTypeSearch search = WebUtils.getParameter(request, GoodsTypeSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        Long total = GoodsTypeService.self().countBySearch(search);
        result.getBody().setTotal(total);
        List<GoodsType> data = GoodsTypeService.self().findBySearch(search,pageNo,pageLimit);
        if (data != null && data.size() > 0) {
            result.setMsg(null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value="/market/cloud/goods/type/tree/list.data")
    public ListResult<GoodsType> treeListData(HttpServletRequest request){
        ListResult<GoodsType> result = new ListResult<GoodsType>(DtoConstants.NULL_DATA, "暂无数据");
        Long parentId = WebUtils.getLong(request,"parentId");
        List<GoodsType> data = GoodsTypeService.self().findByParentId(parentId);
        if (data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route("/market/cloud/goods/type/tree/select.data")
    public TreeResult<GoodsType> select(HttpServletRequest request, HttpServletResponse response){
        TreeResult<GoodsType> result = new TreeResult<GoodsType>(DtoConstants.SUCCESS);
        List<GoodsType> data = GoodsTypeService.self().findAllTree();
        result.getBody().setData(data);
        return result;
    }

    @Route(value = { "/market/cloud/goods/type/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("stateList", DefaultEnum.Status.values());
        request.setAttribute("classifyList", EGoodsClassify.values());
        request.setAttribute("priority",10);
        return "/market/cloud/goods/type/edit.html";
    }

    @Route(value = { "/market/cloud/goods/type/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增商品类型失败");
        GoodsType goodsType = WebUtils.getParameter(request, GoodsType.class);
        if (goodsType != null && StringUtils.isNotBlank(goodsType.getName())
                && StringUtils.isNotBlank(goodsType.getCode())) {
            try {
                if (GoodsTypeService.self().add(goodsType)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/goods/type/item/add.html" })
    public String addItem(HttpServletRequest request) {
        Long parentId = WebUtils.getLong(request,"parentId");
        if(parentId != null){
            GoodsType goodsType = GoodsTypeService.self().getById(parentId);
            if(goodsType != null){
                request.setAttribute("parentId",goodsType.getId());
                request.setAttribute("parentName",goodsType.getName());
                request.setAttribute("parentCode",goodsType.getCode());
            }
        }
        request.setAttribute("stateList", DefaultEnum.Status.values());
        request.setAttribute("classifyList", EGoodsClassify.values());
        request.setAttribute("priority",10);
        return "/market/cloud/goods/type/edit.html";
    }

    @Route(value = { "/market/cloud/goods/type/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        if(id != null){
            GoodsType goodsType = GoodsTypeService.self().getJoinById(id);
            WebUtils.setAttribute(request,goodsType);
        }
        request.setAttribute("stateList", DefaultEnum.Status.values());
        return "/market/cloud/goods/type/edit.html";
    }

    @Route(value = { "/market/cloud/goods/type/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改商品类型失败");
        GoodsType goodsType = WebUtils.getParameter(request, GoodsType.class);
        if (goodsType != null && StringUtils.isNotBlank(goodsType.getName())
                && StringUtils.isNotBlank(goodsType.getCode())) {
            try {
                if (GoodsTypeService.self().updateNotNull(goodsType)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/goods/type/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除商品类型失败");
        Long id = WebUtils.getLong(request,"id");
        if(id != null){
            try {
                if (GoodsTypeService.self().deleteById(id)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
